package boot.spring.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import boot.spring.entity.*;
import boot.spring.mapper.PermissionMapper;
import boot.spring.mapper.RolePermissionMapper;
import boot.spring.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import boot.spring.mapper.RoleMapper;
import boot.spring.service.ModuleService;
import boot.spring.service.PermissionService;
import boot.spring.service.RoleService;

@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleMapper roleMapper;
	
	@Autowired
	private PermissionService permissionService;
	
	@Autowired
	private ModuleService moduleService;

	@Autowired
	private PermissionMapper permissionMapper;

	/*引入角色权限关联表的持久层*/
	@Autowired
	private RolePermissionMapper rolePermissionMapper;

	Result r=new Result();
	
	@Override
	public List<Role> getRoleByUid(String userId) {
		//获取用户拥有的角色
		List<Role> rList = roleMapper.getRoleByUid(userId);
		//当前用户的所有模块
		List<Map<String ,Object>> UserModel = new ArrayList<>();
		//遍历出每个角色
		for(Role r:rList) {
			//获取角色的id
			String roleId = r.getRid();
			System.out.println("++++++"+roleId);
			//根据角色id获取角色的权限
			List<Permission> pList = permissionService.getPermissionByRid(roleId);
			System.out.println();
			System.out.println("++++++"+pList.size());
			/*System.err.println("----------------"+pList.get(0));*/
			//根据角色的id获取角色的模块
			List<Module> mList = moduleService.getModuleByRid(roleId);
			//把权限、模块添加到角色中
			r.setpList(pList);
			r.setmList(mList);
		}
		return rList;
	}





	public List<Map<String ,Object>> getRoleByModel(String userId) {
		//获取用户拥有的角色
		List<Role> rList = roleMapper.getRoleByUid(userId);
		//当前用户的所有模块
		List<Map<String ,Object>> UserModel = new ArrayList<>();
		List<Map<String ,Object>> um_zi = new ArrayList<>();//父模块的子模块

		//遍历出每个角色
		for(Role r:rList) {
			//获取角色的id
			String roleId = r.getRid();
			//根据角色id获取角色的权限
			List<Permission> pList = permissionService.getPermissionByRid(roleId);
			//根据角色的id获取角色的模块
			List<Module> mList = moduleService.getModuleByRid(roleId);
			/*循环取出每一个模块*/
			for(Module m:mList){
				System.out.println("循环取出每一个模块");
				System.out.println(m.getParentid());
				Map<String,Object> um=new HashMap<>();
				if(m.getParentid().equals("i") || m.getParentid()=="i"){//判断是不是父模块，如果是
					System.out.println("判断是不是父模块，如果是");
					um.put("mid",m.getMid());//把父模块的信息存到map里面
					System.out.println("把父模块的信息存到map里面"+m.getMid()+m.getMname());
					um.put("mname",m.getMname());
					um.put("parentid",m.getParentid());
					um.put("url",m.getUrl());
					um.put("weigth",m.getWeight());
					for(Module m_zi:mList){//然后在循环取出当前父模块的子模块
						if(m_zi.getParentid().equals(m.getMid())){
							Map<String,Object> ummap_zi=new HashMap<>();
							ummap_zi.put("mid",m_zi.getMid());//把父模块的信息存到map里面
							ummap_zi.put("mname",m_zi.getMname());
							ummap_zi.put("parentid",m_zi.getParentid());
							ummap_zi.put("url",m_zi.getUrl());
							ummap_zi.put("weigth",m_zi.getWeight());
							um_zi.add(ummap_zi);//把这个父模块的子模块存到list里面
							ummap_zi=null;
						}
					}
					um.put("children",um_zi);
					um_zi= new ArrayList<>();
					UserModel.add(um);
					um=null;

				}
			}
			//把权限、模块添加到角色中
			/*r.setpList(pList);
			r.setmList(UserModel);*/
		}
		return UserModel;
	}

	@Override/*获取所有的角色*/
	public Result RoleMapAll() {
		List<Role> all=roleMapper.RoleMapAll();
		if(all.size()>0){
			r=new Result(0,"请求成功",all.size(),all);
		}else{
			r=new Result(1,"请求失败");
		}
		return r;
	}

	@Override/*新增角色*/
	public Result RoleSerAdd(Role add) {
		Integer addCon=roleMapper.RoleMapAdd(add);
		if(addCon>0){
			r=new Result(0,"新增成功");
		}else{
			r=new Result(1,"新增失败");
		}
		return r;
	}

	@Override/*根据角色名称查询重复的数据*/
	public Result RoleSerrepeatAll(Role all) {
		List<Role> allCon=roleMapper.RoleMaprepeatAll(all);
		if(allCon.size()>0){
			r=new Result(0,"角色名重复");
		}else{
			r=new Result(1,"无重复数据");
		}
		return r;
	}

	@Override/*编辑角色信息*/
	public Result RoleSerUp(Role up) {
		List<Role> allCon=roleMapper.RoleMaprepeatAll(up);/*根据角色名称查询重复的数据*/
		if(allCon.size()>0){
			r=new Result(1,"角色名重复");
		}else{
			Integer upCon=roleMapper.RoleMapUp(up);
			if(upCon>0){
				r=new Result(0,"编辑成功");
			}else{
				r=new Result(1,"编辑失败");
			}
		}

		return r;
	}

	@Override/*删除角色信息*/
	public Result RoleSerDel(Role del) {
		/*删除*/
		/*查询当前角色下有没有模块*/
		List<Module> module =moduleService.getModuleByRid(del.getRid());

		if(module!=null && module.size()>0){
			r=new Result(1,"当前角色下有模块不能删除");
		}else{
			Integer delcon=roleMapper.RoleMapDel(del);
			if(delcon>0){
				r=new Result(0,"删除成功");
			}else{
				r=new Result(0,"删除失败");
			}
		}
		return r;
	}



	/*根据角色的id查询出权限和模块融合返回用于树形数据表格*/
	@Override
	public List<PermissionsModule> ListPmByidSerAll(String roleid) {
		/*创建模块权限融合的实体类的集合*/
		List<PermissionsModule> ListPM=new ArrayList<>();
		ListPM.clear();
		/*获取某个角色的模块*/
		List<Module> Listmodules = moduleService.getModuleByRid(roleid);
		/*获取某个角色的权限*/
		List<Permission> Listpermissions = permissionMapper.ListPermissionMapByidAll(roleid);
		Integer pmsize=0;
		for(int i=0;i<Listmodules.size();i++){
			pmsize+=1;/*作为前端的唯一值*/
			/*把模块的模块id、模块名称、模块父节点id、模块路径存到ListPM*/
			ListPM.add(new PermissionsModule(pmsize,Listmodules.get(i).getMid(),null,Listmodules.get(i).getMname(),Listmodules.get(i).getParentid(),null,Listmodules.get(i).getUrl()));
		}
		for(int i=0;i<Listpermissions.size();i++){
			pmsize+=1;/*作为前端的唯一值*/
			/*把权限的权限id、权限名称、权限的模块id、权限标识符存到ListPM*/
			ListPM.add(new PermissionsModule(pmsize,null,Listpermissions.get(i).getPid(),Listpermissions.get(i).getPname(),Listpermissions.get(i).getMname(),Listpermissions.get(i).getPidentifier(),null));
		}

		return ListPM;
	}

	/*根据模块名称和角色id查询角色权限表里所没有的权限*/
	@Override
	public Result ListModuleRoleIdSerAll(String mname, String roleid) {

		List<Permission> listper=permissionMapper.ListModuleRoleIdMapAll(mname,roleid);
		if (listper.size()>0){
			r=new Result(0,"请求成功",1000,listper);
		}else{
			r=new Result(1,"请求失败");
		}
		return r;
	}

	/*向角色权限关联表里添加数据*/
	@Override
	public Result GetRperSerAdd(RolePermission add) {
		Integer getadd=rolePermissionMapper.GetRperMapAdd(add);
		if(getadd>0){
			r=new Result(0,"新增成功");
		}else{
			r=new Result(0,"新增失败");
		}
		return r;
	}

	/*根据角色id和权限id删除角色权限关联表里的数据*/
	@Override
	public Result GetRperSerDel(RolePermission del) {
		Integer getdel=rolePermissionMapper.GetRperMapDel(del);
		if(getdel>0){
			r=new Result(0,"删除成功");
		}else{
			r=new Result(0,"删除失败");
		}
		return r;
	}
}
